/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import dao.DAOAdded;
import dao.DAOPersonnel;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.plaf.basic.BasicListUI;
import javax.swing.table.DefaultTableModel;
import pojo.AddHistory;
import pojo.Added;
import pojo.PersonnelDetail;

/**
 *
 * @author QTuan
 */
public class QuanLyNhapKho extends javax.swing.JPanel {

    /**
     * Creates new form QuanLyNhapKho
     */
    private PersonnelDetail account= null;
    private String taikhoan = "";
    private DefaultTableModel modelAdded = new DefaultTableModel();
    private DefaultTableModel modelHistory = new DefaultTableModel();
    public QuanLyNhapKho(PersonnelDetail account,String taikhoan) {
        initComponents();
        modelAdded.addColumn("Mã hóa đơn");
        modelAdded.addColumn("Ngày nhập hàng");
        modelAdded.addColumn("Ngày tạo hóa đơn");
        modelAdded.addColumn("Người tạo");
        modelHistory.addColumn("Thời gian");
        modelHistory.addColumn("Mã hóa đơn");
        modelHistory.addColumn("Người thực hiện");
        modelHistory.addColumn("Hành động");
        this.jTable1.setModel(modelAdded);
        this.jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.jTable2.setModel(modelHistory);
        DAOAdded daoadded = new DAOAdded();
        List<Added> dsHoaDon = daoadded.getAllAdded();
        for(int i=0;i<dsHoaDon.size();i++)
        {
            modelAdded.addRow(new Object[]{dsHoaDon.get(i).getId(),dsHoaDon.get(i).getDtimeAdd(),dsHoaDon.get(i).getDtimeCreate(),dsHoaDon.get(i).getPerAccount()});
        }
        
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jLabel1.setText("Danh sách hóa đơn");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel2.setText("Lịch sử hóa đơn");

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jButton1.setText("Lịch sử");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("ReLoad");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(439, 439, 439)
                        .addComponent(jButton1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap(195, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int select = this.jTable1.getSelectedRow();
        if(select<0)
        {
            JOptionPane.showMessageDialog(null, "Bạn chưa chọn hóa đơn","Chưa chọn",JOptionPane.WARNING_MESSAGE);
        }else
        {
            for(int i=modelHistory.getRowCount()-1;i>=0;i--)
            {
                modelHistory.removeRow(0);
            }
            DAOAdded daoadd = new DAOAdded();
            Added add = daoadd.getAddedById((Integer)modelAdded.getValueAt(select, 0));
            List<AddHistory> his = new ArrayList<AddHistory>(add.getAddHistories());
            for(int i=0;i<his.size();i++)
            {
                modelHistory.addRow(new Object[]{his.get(i).getDtime(),add.getId(),his.get(i).getAccount(),his.get(i).getAction()});
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        DAOAdded daoadded = new DAOAdded();
        List<Added> dsHoaDon = daoadded.getAllAdded();
        for(int i=modelAdded.getRowCount()-1;i>=0;i--)
            {
                modelAdded.removeRow(0);
            }
        for(int i=0;i<dsHoaDon.size();i++)
        {
            modelAdded.addRow(new Object[]{dsHoaDon.get(i).getId(),dsHoaDon.get(i).getDtimeAdd(),dsHoaDon.get(i).getDtimeCreate(),"admin"});
        }
    }//GEN-LAST:event_jButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables
}
